package com.irs.irsforum.config;

import com.irs.irsforum.security.AuthStudentInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
 * @author Liyihui
 * @Date: 2020/3/22
 * @Time: 3:09
 */
@Slf4j
@Configuration
public class LoginConfiguration implements WebMvcConfigurer {

    @Bean
    AuthStudentInterceptor authStudentInterceptor() {
        return new AuthStudentInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(authStudentInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/")
                .excludePathPatterns("/post/showpostlist")
                .excludePathPatterns("/post/pagemessage")
                // 排除资源请求
                .excludePathPatterns("/css/login/*.css")
                .excludePathPatterns("/js/login/**/*.js")
                .excludePathPatterns("/image/login/*.png");

    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowCredentials(true)
                .allowedMethods("GET", "POST", "DELETE", "PUT", "PATCH", "OPTIONS")
                .maxAge(3600);
    }

}
